/**
 * 
 */
package gui.controller;

import gui.view.EditKlassenItemSet;
import helden.plugin.ausruestungsplugin.PluginHeldenAusruestungWerkzeug2;
import helden.plugin.werteplugin2.PluginGegenstand;

import java.awt.Window;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import java.util.Collections;

import javax.swing.DefaultListModel;
import javax.swing.DropMode;
import javax.swing.JOptionPane;

import backend.DatenController;

/**
 * @author Christian
 * 
 */
public class EditKlassenController {
	
	private EditKlassenItemSet dialog;
	
	private PluginHeldenAusruestungWerkzeug2 phaw;
	
	private MainController controller;
	
	private String oldSetName;
	
	public EditKlassenController() {

		super();
	}
	
	public EditKlassenController(Window window, PluginHeldenAusruestungWerkzeug2 phaw,
			MainController controller) {

		this();
		this.phaw = phaw;
		this.controller = controller;
		dialog = new EditKlassenItemSet(window, this);
		init();
		addCloseAction();
		dialog.getJSetItemList().setDropMode(DropMode.INSERT);
		dialog.getJSetItemList().setTransferHandler(
				new ListTransferHandler(dialog.getJSetItemList()));
		dialog.setSize(580, 375);
		dialog.setLocationRelativeTo(window);
		dialog.setVisible(true);
	}
	
	/**
	 * @param dialog2
	 * @param selectedItem
	 */
	public EditKlassenController(Window window, PluginHeldenAusruestungWerkzeug2 phaw,
			MainController controller, String selectedItem) {

		this(window, phaw, controller);
		dialog.getJNameTextField().setText(selectedItem);
		oldSetName = selectedItem;
		DefaultListModel model = (DefaultListModel) dialog.getJSetItemList().getModel();
		model.clear();
		for (String item : DatenController.getInstance().getKlassenItemList(selectedItem)) {
			model.addElement(item);
		}
	}
	
	private void init() {

		setAlleGegenstaende();
		suchen();
	}
	
	private void setAlleGegenstaende() {

		DefaultListModel model = (DefaultListModel) dialog.getJAlleItemsList().getModel();
		model.clear();
		ArrayList<String> list = new ArrayList<String>();
		for (PluginGegenstand s : phaw.getAlleGegenstaende()) {
			list.add(s.getName());
		}
		Collections.sort(list);
		for (String s : list) {
			if (s.toLowerCase().contains(dialog.getJSuchTextFeld().getText().toLowerCase())
					|| dialog.getJSuchTextFeld().getText().equals("")) {
				model.addElement(s);
			}
		}
	}
	
	public void delGegenstand() {

		String selectedItem = (String) dialog.getJSetItemList().getSelectedValue();
		DefaultListModel model = (DefaultListModel) dialog.getJSetItemList().getModel();
		model.removeElement(selectedItem);
		dialog.getJSetItemList().revalidate();
	}
	
	public void suchen() {

		if (dialog != null) {
			setAlleGegenstaende();
		}
	}
	
	public void speichern() {

		boolean used = DatenController.getInstance().isNameInBenutzung(
				dialog.getJNameTextField().getText());
		if (dialog.getJNameTextField().equals(oldSetName) || !used) {
			if (!dialog.getJNameTextField().getText().trim().equals("")) {
				ArrayList<String> list = new ArrayList<String>();
				DefaultListModel model = (DefaultListModel) dialog.getJSetItemList().getModel();
				for (int i = 0; i < model.size(); i++) {
					list.add((String) model.getElementAt(i));
				}
				if(!dialog.getJNameTextField().equals(oldSetName)) {
					DatenController.getInstance().entferneItemSet(oldSetName);
				}
				DatenController.getInstance().setKlassenItemList(
						dialog.getJNameTextField().getText(), list);
				dialog.dispose();
				controller.initLists();
			} else {
				JOptionPane.showMessageDialog(dialog, "Der Setname darf nicht Leer sein!",
						"Name schon vergeben", JOptionPane.WARNING_MESSAGE);
			}
		} else {
			JOptionPane.showMessageDialog(dialog, "Jeder Setname darf nur 1 mal vorkommen!",
					"Name schon vergeben", JOptionPane.WARNING_MESSAGE);
		}
	}
	
	public void close() {

		dialog.dispose();
	}
	
	private void addCloseAction() {

		dialog.addWindowListener(new WindowAdapter() {
			
			@Override
			public void windowClosing(WindowEvent e) {

				dialog.dispose();
			}
		});
	}
}
